package com.testyourshell.model.user;

import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.testyourshell.data.Role;
import com.testyourshell.model.IdentifiedEntity;
import com.testyourshell.model.person.Person;

@Entity
@Table(name = "user")
@NamedQueries({ @NamedQuery(name = "User.findByUsername", query = "FROM User usr WHERE usr.username = :username") })
public class User extends IdentifiedEntity {

	private static final long serialVersionUID = 1L;

	@Column(name = "username", length = 32, nullable = false, unique = true)
	private String username;

	@Column(name = "password", nullable = false)
	private String password;

	@JoinColumn(name = "person_id", nullable = false)
	@OneToOne(cascade = { CascadeType.ALL }, optional = false)
	private Person person;

	@ElementCollection(targetClass = Role.class)
	@CollectionTable(name = "user_roles")
	@Column(name = "role")
	@Enumerated(EnumType.ORDINAL)
	private Collection<Role> roles;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Collection<Role> getRoles() {
		return roles;
	}

	public void setRoles(Collection<Role> roles) {
		this.roles = roles;
	}
}
